using UnityEngine;
using System.Collections.Generic;

namespace Emerald4
{
    /// <summary>
    /// Simple registry for tablet apps. No GameObject needed!
    /// Just call TabletAppRegistry.Register() from any script.
    /// </summary>
    public static class TabletAppRegistry
    {
        private static List<TabletAppConfig> _registeredApps = new List<TabletAppConfig>();
        private static SomniumTabletManager _manager;

        public class TabletAppConfig
        {
            public string buttonTitle;
            public Sprite buttonIcon;
            public GameObject appPrefab;
            public bool autoResizeOnOpen = true;
            public Vector3 positionOffset = Vector3.zero;
        }

        /// <summary>
        /// Register a tablet app from anywhere in your code
        /// </summary>
        public static void Register(string title, Sprite icon, GameObject prefab, bool autoResize = true, Vector3 positionOffset = default)
        {
            // Check if already registered
            if (IsRegistered(title))
            {
                Debug.LogWarning($"[TabletAppRegistry] App '{title}' is already registered");
                return;
            }

            var config = new TabletAppConfig
            {
                buttonTitle = title,
                buttonIcon = icon,
                appPrefab = prefab,
                autoResizeOnOpen = autoResize,
                positionOffset = positionOffset
            };

            _registeredApps.Add(config);

            // If manager already exists, register immediately
            if (_manager != null)
            {
                _manager.CreateAppButton(config);
            }

            Debug.Log($"[TabletAppRegistry] Registered: {title}");
        }

        /// <summary>
        /// Unregister a tablet app by title
        /// </summary>
        public static void Unregister(string title)
        {
            // Find and remove from registered apps
            TabletAppConfig configToRemove = null;
            foreach (var config in _registeredApps)
            {
                if (config.buttonTitle == title)
                {
                    configToRemove = config;
                    break;
                }
            }

            if (configToRemove != null)
            {
                _registeredApps.Remove(configToRemove);

                // If manager exists, remove the button
                if (_manager != null)
                {
                    _manager.RemoveAppButton(title);
                }

                Debug.Log($"[TabletAppRegistry] Unregistered: {title}");
            }
        }

        /// <summary>
        /// Check if an app with the given title is registered
        /// </summary>
        public static bool IsRegistered(string title)
        {
            foreach (var config in _registeredApps)
            {
                if (config.buttonTitle == title)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Internal: Called by manager when it's ready
        /// </summary>
        internal static void NotifyManagerReady(SomniumTabletManager manager)
        {
            _manager = manager;

            // Register all pending apps
            foreach (var config in _registeredApps)
            {
                _manager.CreateAppButton(config);
            }
        }

        /// <summary>
        /// Clear all registered apps (useful for testing)
        /// </summary>
        public static void Clear()
        {
            _registeredApps.Clear();
        }
    }
}
